package com.maker.ai.repository;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ChatMessageDeserializer;
import dev.langchain4j.data.message.ChatMessageSerializer;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.time.Duration;
import java.util.List;

@Repository
public class RedisChatMemoryStore implements ChatMemoryStore {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public static final String CHAT_MEMORY_PREFIX = "maker-chat-memory:";


    @Override
    public List<ChatMessage> getMessages(Object memoryId) {
        String json =redisTemplate.opsForValue().get(CHAT_MEMORY_PREFIX+memoryId);
        List<ChatMessage> messageList = ChatMessageDeserializer.messagesFromJson(json);
        return messageList;
    }

    @Override
    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
        String json = ChatMessageSerializer.messagesToJson(messages);
        redisTemplate.opsForValue().set(CHAT_MEMORY_PREFIX+memoryId.toString(),json, Duration.ofDays(1));
    }

    @Override
    public void deleteMessages(Object memoryId) {
        redisTemplate.delete(CHAT_MEMORY_PREFIX+memoryId.toString());
    }







}
